Skip to content

[gremlin-python] [1/4] Add async HTTP streaming transport, AsyncResultSet, and GraphBinaryV4 stream reader - #3725

Open
masterhugo wants to merge 1 commit into
apache:masterfrom
masterhugo:feature/tp4-async-pr1-transport
Open

masterhugo wants to merge 1 commit into
apache:masterfrom
masterhugo:feature/tp4-async-pr1-transport

Conversation

@masterhugo

Copy link
Copy Markdown
Contributor

Summary (Part 1 of 4: Native asyncio for gremlin-python 4.0)

This is PR 1 of 4 introducing native asyncio support to gremlin-python for TinkerPop 4.0 over HTTP streaming, without nest_asyncio, background ThreadPoolExecutor pools, or per-connection private event loops (asyncio.new_event_loop()).

Why native asyncio without nest_asyncio?

Currently, AiohttpHTTPTransport creates a private event loop per connection and drives aiohttp synchronously via run_until_complete(). When called from inside an already-running event loop (FastAPI/uvloop, Jupyter notebooks, async AWS Lambda handlers), it fails with RuntimeError: Cannot run the event loop while another loop is running, and nest_asyncio cannot patch Cython-based loops like uvloop.

What is included in PR 1 (Foundation layer)

  • gremlin_python/driver/aiohttp/async_transport.py:
    • AsyncBufferedStream: sliding-window bytearray buffer over aiohttp.ClientResponse.content with checkpoint(), restore(), compact(), fill_more(), and read_exactly(). Translates aiohttp.ServerTimeoutError into ReadTimeoutError.
    • AsyncAiohttpHTTPTransport: native aiohttp.ClientSession transport bound directly to asyncio.get_running_loop().
  • gremlin_python/driver/async_resultset.py:
    • AsyncResultSet: single-item streaming result set supporting await rs.one() (returning _EXHAUSTED on stream end so None result items are preserved), await rs.all(), and async for item in rs, using asyncio.wait(..., return_when=FIRST_COMPLETED) instead of polling loops.
  • gremlin_python/driver/serializer.py:
    • Added GraphBinarySerializersV4.deserialize_async_response_stream(stream) to incrementally decode GraphBinary v4 (0x84) objects and end-of-stream trailers across arbitrary HTTP chunk boundaries.
  • tests/unit/driver/test_async_http_streaming.py:
    • 27 unit tests covering AsyncResultSet, AsyncBufferedStream, deserialize_async_response_stream (including 2-byte chunk fragmentation across object/trailer boundaries), and AsyncAiohttpHTTPTransport.

Roadmap of the 4-PR Series

  • PR 1 (this PR): AsyncBufferedStream, AsyncAiohttpHTTPTransport, AsyncResultSet, deserialize_async_response_stream
  • PR 2: AsyncConnection & AsyncClient (asyncio.Queue connection pool)
  • PR 3: AsyncDriverRemoteConnection & AsyncGraphTraversal (async_traversal())
  • PR 4: AsyncTransaction (g.tx(), execute_in_tx), integration tests, examples, and documentation

@codecov-commenter

codecov-commenter commented Oct 19, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 76.75%. Comparing base (a28cd1f) to head (a3381f0).
⚠️ Report is 877 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #3725      +/-   ##
============================================
+ Coverage     76.35%   76.75%   +0.39%     
- Complexity    13424    14357     +933     
============================================
  Files          1012     1037      +25     
  Lines         60341    64819    +4478     
  Branches       7075     7704     +629     
============================================
+ Hits          46076    49753    +3677     
- Misses        11548    11967     +419     
- Partials       2717     3099     +382     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@masterhugo
masterhugo force-pushed the feature/tp4-async-pr1-transport branch from 60886f4 to a3381f0 Compare October 20, 2026 00:36
@kenhuuu

kenhuuu commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

Thanks for contributing @masterhugo and sorry for the delayed response. I'll be taking a look at your other 3.x version of this PR soon but I wanted to let you know that the approach you are taking here probably isn't the right direction.

4.x is the next major release and we would probably need to come a decision on what async APIs look like in TinkerPop (this affects gremlin-dotnet and gremlin-javascript as well). We also need to decide if we are going to support both async and sync APIs for gremlin-python and what a truly native async API might look like. These are decisions that have to take place on the dev list. While we can talk about it here and on Discord (I noticed you posted there as well), a DISCUSS thread has to occur on the dev list for any official decisions.

@kenhuuu

kenhuuu commented Oct 23, 2026

Copy link
Copy Markdown
Contributor

Just to clarify a few things from my earlier post. While I think the changes made in your 3.x PR work for 3.x, I don't think they are the right decisions for 4.x given that we can take a more holistic approach with async since breaking changes are allowed in the master branch.

Getting async to work natively in the gremlin-python driver is a fairly common request so I think it deserves discussion. I was wondering if you were willing to start that discussion with how you would ideally want it to work in 4.x and how that could be applied throughout the TinkerPop ecosystem.

@masterhugo

Copy link
Copy Markdown
Contributor Author

Thanks for clarifying @kenhuuu! Initially I took this approach mainly to avoid breaking what was already in master, but if breaking changes are on the table for 4.x, I'd definitely prefer to build something natively async from scratch and move away from the sync layer in the driver altogether.

I already have an open [DISCUSS] thread on the dev list from when I started working on TINKERPOP-2774: https://lists.apache.org/thread/wm5d9hnx5jkt8mgzqg4g8vtplhohrh3n

We can continue the discussion there (or I can post a follow-up in that thread) to figure out the best way to design and implement a fully native async driver from the ground up for 4.x.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants